import 'package:air_audio/global.dart';
import 'package:air_audio/manager.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:sleek_circular_slider/sleek_circular_slider.dart';

class SleepPage extends StatefulWidget {
  @override
  _SleepPageState createState() => _SleepPageState();
}

class _SleepPageState extends State<SleepPage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    MainPlayerState playerState = Provider.of<MainPlayerState>(context);
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        Text(
          '设置定时关闭',
          style: TextStyle(
            color: Global.fontColor,
            fontSize: 17,
            fontWeight: FontWeight.w600,
          ),
        ),
        SizedBox(height: 10),
        SleekCircularSlider(
          appearance: CircularSliderAppearance(
            size: 250,
            customWidths: CustomSliderWidths(progressBarWidth: 20),
            customColors: CustomSliderColors(
              trackColor: Global.fontColor.withOpacity(.3),
              progressBarColor: Global.fontColor.withOpacity(.3),
              shadowColor: Global.fontColor.withOpacity(.3),
              dotColor: Global.fontColor.withOpacity(.3),
            ),
          ),
          min: 0,
          max: 120,
          initialValue: playerState.timing,
          onChange: (double value) {
            playerState.setSliderValue(value);
          },
          innerWidget: (double value) {
            return Container(
              child: Center(
                child: Text(
                  _formatIntervalTime(value),
                  style: TextStyle(
                    color: Global.fontColor,
                    fontSize: 24,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            );
          },
        ),
        FlatButton(
          child: Text(playerState.timer == null ? '开启定时' : '关闭定时'),
          color: Global.fontColor.withOpacity(.3),
          textColor: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(50.0),
          ),
          onPressed: playerState.setTimer,
        ),
      ],
    );
  }

  String _formatIntervalTime(double init) {
    var sleepTime = init ~/ 1;
    var hours = sleepTime ~/ 60;
    var minutes = sleepTime % 60;
    if (hours == 0) {
      return '$minutes分钟';
    } else if (minutes == 0) {
      return '$hours小时';
    } else {
      return '$hours小时$minutes分钟';
    }
  }
}
